<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page="page"
               @row-del="rowDel"
               v-model="form"
               :permission="permissionList"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.appanimal_delete"
                   @click="handleDelete">删 除
        </el-button>

<!--        <el-button size="small"-->
<!--                   icon="el-icon-delete"-->
<!--                   @click="handlerMap"-->
<!--                   plain>权限设置-->
<!--        </el-button>-->
      </template>
    </avue-crud>


    <el-dialog title="提示"
               :visible.sync="map"
               height="900px"
               width="1200px">
           <mapIframe :src="src"></mapIframe>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="map = false">取 消</el-button>
        <el-button type="primary"

                  >确 定</el-button>

      </span>
    </el-dialog>


  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/app/appanimal";
  import {mapGetters} from "vuex";
  import mapIframe from '@/components/map/main';

  export default {
    components: {mapIframe},
    data() {
      return {
        src:"http://127.0.0.1:81/web/cal",
        map:false,
        form: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "id",
              prop: "id",
              hide:true,
              addDisplay:false,
              editDisplay:false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "名称",
              search:true,
              prop: "name",
              span:24,
              rules: [{
                required: true,
                message: "请输入名称",
                trigger: "blur"
              }]
            },
            {
              label: "更新时间",
              prop: "updateDate",
              addDisplay:false,
              editDisplay:false,
              rules: [{
                required: true,
                message: "请输入CURRENT_TIMESTAMP",
                trigger: "blur"
              }]
            },
            {
              label: "类型",
              prop: "type",
              type:"select",
              width:135,
              dicUrl:"/api/blade-system/dict/dictionary?code=animalCategory",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入类型",
                trigger: "blur"
              }]
            },
            {
              label: "数量",
              prop: "num",
              width:80,
              rules: [{
                required: true,
                message: "请输入数量",
                trigger: "blur"
              }]
            },
            {
              label: "图片",
              hide:true,
              prop: "textImgs",
              type: 'upload',
              listType: 'picture-img',
              span: 24,
              propsHttp: {
                res: 'data'
              },
              tip: '只能上传jpg/png文件，且不超过500kb',
              action: '/api/app/upload/imgs'
            },
            {
              label: "描述",
              prop: "textDesc",
              type:"textarea",
              width:500,
              minRows:5,
              span:24,
              rules: [{
                required: true,
                message: "请输入描述",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createDate",
              addDisplay:false,
              editDisplay:false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },

          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.appanimal_add, false),
          viewBtn: this.vaildData(this.permission.appanimal_view, false),
          delBtn: this.vaildData(this.permission.appanimal_delete, false),
          editBtn: this.vaildData(this.permission.appanimal_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handlerMap(){
        this.map=true
      },
      rowSave(row, loading) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      },
      rowUpdate(row, index, loading) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },

      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      onLoad(page, params = {}) {
        getList(page.currentPage, page.pageSize, params).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
        });
      }
    }
  };
</script>

<style>
  /*单行文本的溢出显示省略号*/
  .pl{
    width: 200px;
    overflow:hidden;
    text-overflow:ellipsis;
    background: goldenrod;
    white-space: nowrap;/*加宽度width属来兼容部分浏览*/
  }
</style>
